import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
// import DestinationView from "../views/DestinationView.vue";
// import TourismStrategyView from "../views/TourismStrategyView.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: () => import("../views/HomeView.vue"),
  },
  {
    path: "/destination",
    name: "destinationView",
    component: () => import("../views/DestinationView.vue"),
  },
  {
    path: "/tourismstrategy",
    name: "tourismstrategy",
    component: () => import("../views/TourismStrategyView.vue"),
  },
  {
    path: "/traveldiary",
    name: "traveldiary",
    component: () => import("../views/TravelDiaryView.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../components/LoginView.vue"),
  },
  {
    path: "/register",
    name: "register",
    component: () => import("../components/RegisterView.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 先把VueRouter原型对象的push replace  方法保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// 重写push方法 第一个参数
VueRouter.prototype.push = function (location, resolve, reject) {
  // console.log(this);//实例
  // 判断如果成功或失败回调已经传了
  if (resolve && reject) {
    // call 调用函数一次  改变this指向
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    );
  }
};

export default router;
